package cn.hniu.controller;

import cn.hniu.mapper.VideotPostsMapper;
import cn.hniu.pojo.TextPosts;
import cn.hniu.pojo.Users;
import cn.hniu.pojo.VideotPosts;
import cn.hniu.pojo.util.PageAll;
import cn.hniu.pojo.util.Result;
import cn.hniu.service.VideotPostsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/VideotPosts")
public class VideotPostsController {
    @Autowired
    private VideotPostsService videotPostsService;
    @Autowired
    private VideotPostsMapper videotPostsMapper;

    //查询帖子
    @GetMapping("/{data}/{begin}/{size}")
    @ResponseBody
    public Result findVideotPosts(@PathVariable("data") String data,
                                @PathVariable("begin") Integer begin,
                                @PathVariable("size") Integer size){
        if(videotPostsService.findVideotPosts(data,begin-1,size).size()==0){
            return new Result(400,"没有查到");
        }else {
            int list = videotPostsMapper.selectCount(new QueryWrapper<VideotPosts>().like("v_title", data));
            PageAll pageAll=new PageAll();
            pageAll.setCurrent(begin);
            pageAll.setPages((int)Math.ceil((double)list/size));
            pageAll.setSize(size);
            pageAll.setTotals(list);
            pageAll.setVideotPostsList(videotPostsService.findVideotPosts(data,begin-1,size));
            return new Result(200,pageAll);
        }
    }
    //修改帖子
    @PutMapping("/updateVideotPosts")
    @ResponseBody
    public Result updateVideotPosts(@RequestBody VideotPosts videotPosts){
        if(videotPostsService.updateVideotPosts(videotPosts)>0){
            return new Result(200,"修改成功");
        }else {
            return new Result(400,"修改失败");
        }
    }
    //删除帖子
    @PutMapping("/deleteVideotPosts/{id}")
    @ResponseBody
    public Result deleteVideotPosts(@PathVariable("id") Integer id){
        if(videotPostsService.deleteVideotPosts(id)>0){
            return new Result(200,"删除成功");
        }else {
            return new Result(400,"删除失败");
        }
    }
    //查询帖子浏览量 后端 查询 帖子表 以热度排序为前十的记录
    @GetMapping("/findTextPostsBytHot")
    public Result findTextPostsBytHot(){
        if(videotPostsService.findVideotPostsBytHot().size()==0){
            return new Result(400,"没查到");
        }else {
            return new Result(200,videotPostsService.findVideotPostsBytHot());
        }
    }
}
